<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :md="4" :sm="8">
            <a-form-item
              label="项目名称"
              :maxLength="50">
              <a-input placeholder="请输入项目名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="项目类别"  :maxLength="50">
              <a-select v-model="queryParam.types" placeholder="请选择项目类别">
                <a-select-option v-for="(item) in this.$sysDictOptions('repairTypes')" :key="item.k" :value="parseInt(item.k)">
                  {{ item.v }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="项目分类"   :maxLength="50">
              <a-tree-select
                      v-model="queryParam.equType"
                      style="width: 100%"
                      :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                      :tree-data="typeTree"
                      placeholder="请选择"
              >
              </a-tree-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="地点"   :maxLength="50">
              <a-cascader
                      v-if="optionsRoom.length"
                      v-model="roomValue"
                      :options="optionsRoom"
                      @change="handleChange"
                      change-on-select
                      placeholder="地点"
              />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" icon="search">查询</a-button>
              <a-button
                @click="() => {this.$objToReset(queryParam),this.roomValue=[],$refs.table.refresh(true)}"
                icon="reload"
                style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <div class="table-operator">
        <a-button @click="$refs.modalEdit.add()" type="primary" icon="plus" v-wx-role="40201">添加</a-button>
        <a-button type="primary" icon="upload" @click="$refs.fileEdit.edit()">导入</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item>
              <a-popconfirm
                  title="确定删除吗?"
                  @confirm="() => handleDelete()"
                  v-wx-role="40204">
                <a><a-icon type="delete"/>删除</a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">批量操作<a-icon type="down"/></a-button>
        </a-dropdown>
      </div>
      <s-table
        ref="table"
        rowKey="id"
        size="middle"
        :scroll="{ x: 1500 }"
        :columns="columns"
        :alert="options.alert"
        :row-selection="options.rowSelection"
        :customRow="rowClick"
        :data="loadData">
        <template slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-wx-role="40202">
            <a-icon type="edit"/>编辑</a>
          <a-divider type="vertical"/>
          <a @click="handelView(record)" v-wx-role="40204"><a-icon/> 维修明细</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() =>handleDelete(record.id)" v-wx-role="40203">
            <a>删除</a>
          </a-popconfirm>
        </template>
      </s-table>

      <equ-repair-edit ref="modalEdit" @ok="modalEditOk"></equ-repair-edit>
      <equ-repair-view ref="modalView"></equ-repair-view>
      <file-repaire-upload ref="fileEdit" @ok="modalEditOk"></file-repaire-upload>
    </div>
  </a-card>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import { deleteAction, postAction,getAction } from '@/api/manage'
  import { mixin } from '@/utils/mixin'
  import EquRepairEdit from './EquRepairEdit'
  import EquRepairView from './EquRepairView'
  import FileRepaireUpload from "./fileRepaireUpload";

  export default {
    name: 'EquRepairList',
    mixins: [mixin],
    components: {FileRepaireUpload, STable, Ellipsis, EquRepairEdit, EquRepairView },
    data () {
      return {
        visible: false,
        // 查询条件
        queryParam: {
          title: ''
        },
        // 表头
        // 如果需要字典，那么需要在columns列中加入 customRender: (text) => this.$sysDictFilter(text, 'xxxx')xxxx 为dataIndex
        columns: [
            {
                title: '编号',
                width:50,
                dataIndex: 'no'
            },
            {
                title: '年度',
                width:50,
                dataIndex: 'year'
            },
            {
                title: '项目分类',
                width:150,
                dataIndex: 'equTypeName'
            },
          {
            title: '项目名称',
              width:210,
            dataIndex: 'name'
          },
          {
            title: '项目类别',
            dataIndex: 'types',
              width:100,
              customRender: (text) => this.$sysDictFilter(text, 'repairTypes')
          },
            {
                title: '地点',
                width:100,
                dataIndex: 'addressS',
                customRender: (text,record) =>{
                    var ads = '';
                    if(record.campusAddress){
                        ads=""+record.campusAddress;
                    }
                    if(record.buildings){
                        ads=""+record.buildings;
                    }
                    return ads
                }
            },
          {
            title: '可研评审文号',
              width:190,
            dataIndex: 'fsReviewNumber'
          },
          {
            title: '可研批复时间',
              width:150,
            dataIndex: 'fsAppTime'
          },
          {
            title: '可研批复文号',
              width:190,
            dataIndex: 'fsAppNumber'
          },
          {
            title: '可研批复估算',
              width:150,
            dataIndex: 'fsAppEst'
          },
          {
            title: '初设评审文号',
              width:190,
            dataIndex: 'priReviewNumber'
          },
          {
            title: '初设批复时间',
              width:150,
            dataIndex: 'priAppTime'
          },
          {
            title: '初设批复文号',
              width:190,
            dataIndex: 'priAppNumber'
          },
          {
            title: '初设批复估算',
              width:150,
            dataIndex: 'priAppEst'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
              width:240,
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          console.log('loadData.parameter', parameter)
          return postAction(this.url.list, Object.assign(parameter, this.queryParam))
              .then(res => {
                return res.result
              })
        },
        selectedRowKeys: [],
        options: {
          alert: {
            clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            type: 'checkbox'
          }
        },
          typeTree:[],
          optionsRoom:[],
          roomValue:[],
        url: {
          list: '/system/equRepair/list',
            listTree: '/system/devCampusBuildingFloorRoom/listTree',
            treelist: '/system/equType/tree',
          delete: '/system/equRepair/delete'
        }
      }
    },
    computed: {},
      created () {
          this.getRoomListTree()
          this.getTypeTree(true)
      },
    methods: {
      // 删除
      handleDelete (record) {
        if (!record) {
          // 如果没有传入，那么批量删除
          record = this.selectedRowKeys.join(',')
        }
        deleteAction(this.url.delete, { ids: record }).then((resp) => {
          this.$message.success('删除成功!')
          this.$refs.table.refresh(true)
        })
      },
        getRoomListTree() {
            getAction(this.url.listTree).then((res) => {
                this.optionsRoom = res.result.data
            })
        },
        getTypeTree (firstFlag) {
            getAction(this.url.treelist,{}).then(res => {
                if (firstFlag) {
                    if(res.result.data[0].children) {
                        const treeLv2 = res.result.data[0].children.map((item, index, arr) => {
                            const treeLv2Tmp = (item.key)
                            return treeLv2Tmp
                        })
                        this.expandedKeys = treeLv2
                        expandedKeySource = treeLv2
                    }
                }
                this.typeTree = res.result.data
            })
        },
      // 用户编辑
      handleEdit (record) {
        this.$refs.modalEdit.edit(record)
      },
      searchQuery () {
        this.$refs.table.refresh(true)
      },
      // 保存刷新
      modalEditOk () {
        this.$refs.table.refresh()
      },
      // 查看
      handelView (record) {
        this.$refs.modalView.show(record)
      },
        rowClick(record, index) {
            return {
                on: {
                    click: () => {

                    },
                    dblclick: () => {
                      this.handelView(record)
                    },
                }
            }
        },
      //开启选择
      onSelectChange (selectedRowKeys) {
         console.log('selectedRowKeys changed: ', selectedRowKeys)
         this.selectedRowKeys = selectedRowKeys
      },
        handleChange(value, selectedOptions) {
            console.log(value)
            if(value){
                this.queryParam.roomValue = value.join(",")
            }
        }
    },
    watch: {}
  }
</script>
<style scoped>
</style>
